#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int IN=1e8,OUT=5e7;
char _i[IN],*_I=_i,_o[OUT],*_O=_o,__[20];
inline int rd(){
	for(;*_I<0x30;_I++);int x=0;
	for(;*_I>0x2f;_I++)x=(x<<1)+(x<<3)+(*_I^0x30);
	return x;
}
inline void wr(ll x){
	int l=0;
	do __[l++]=0x30|x%10;while(x/=10);
	while(l--)*_O++=__[l];*_O++=0xa;
}
namespace MAOJUN{

const int N=1e6+5;
int n,a[N],b[N];

inline void slv(){
	n=rd();
	for(int i=1;i<=n;i++)a[i]=rd();
	for(int i=1;i<=n;i++)b[i]=rd();
	ll x=0,y=0,z=0,w=0,s=0;
	for(int i=1;i<=n;i++)
		if(!a[i]){
			if(b[i]){*_O++=0x2d;*_O++=0x31;*_O++=0xa;return;}
		}else if(!b[i]){
			if(x<a[i]){
				ll d=a[i]-x;s+=d;x+=d;
				ll c=min(d,z);z-=c;w+=c;
			}
		}else{
			x=min(x,a[i]-1ll);a[i]+=-x+y;
			if(a[i]<b[i]){
				ll d=b[i]-a[i];
				if(x<d){
					ll t=d-x;y+=t;s+=t;d-=t;
					ll c=min(t,w);w-=c;s-=c;
				}
				x-=d;z+=d;
				ll c=min(d,w);w-=c;x+=c;y+=c;z-=c;
			}else{
				ll d=a[i]-b[i];
				if(y<d){
					ll c=d-y;s+=c;x+=c;a[i]-=c;d-=c;
				}
				z+=min(d,a[i]-1-y);y-=d;
			}
			z=min(z+w,b[i]-1-y);w=0;
		}
	wr(s);
}
inline void main(){for(int T=rd();T--;slv());}

}int main(){
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	fread(_i,1,IN,stdin);
	MAOJUN::main();
	fwrite(_o,1,_O-_o,stdout);
	return 0;
}